(function(window){
	function Player($audio){
		return new Player.prototype.init($audio);
	}
	Player.prototype={
		constructor: Player,
		musicList:[],
		init:function($audio){
			this.$audio=$audio;
			this.audio=$audio.get(0);
		},
		currentIndex:-1,
		playMusic:function(index,music){
			//判断是否为同一首音乐
			if(this.currentIndex==index){
				//同一首音乐
				if(this.audio.paused){
					this.audio.play();
				}else{
					this.audio.pause();
				}
			}
			else{
					//不是同一首
					this.$audio.attr("src",music.link_url);
					this.audio.play();
					this.currentIndex=index;
				}
		}
	   ,preIndex:function(){
	   	var index=this.currentIndex-1;
	   	if(index<0){
	   		index=this.musicList.length-1;
	   	}
	   	return index;
	   },
	   
	   nextIndex:function(){
	   	var index=this.currentIndex+1;
	   	if(index>this.musicList.length-1){
	   		index=0;
	   	}
	   	return index;
	   }
	   
	   ,changeMusic:function(index){
	   	//删除对应的数据
	   	this.musicList.splice(index,1);
	   	//判断当前删除的是否在正在播放音乐的签名   避免下一首按钮的bug
	   	if(index<this.currentIndex){
	   		this.currentIndex=this.currentIndex-1;
	   		
	   	}
	   },
//	   getMusicDuration:function(){
//	   	return this.audio.duration;//duration方法获取原生aduio的总时长
//	   },
//	   getMusicCurrentTime:function(){
//	   	return this.audio.currentTime;//currentTime方法获取原生aduio的播放进度时长
//	   },
	   musicTimeUpdate:function(callback){
	   	var $this=this;
	   	//8.监听播放的进度
	         this.$audio.on("timeupdate",function(){        	
	         	var duration= $this.audio.duration;
	         	var currentTime= $this.audio.currentTime;
	         	var timeStr= $this.formatDate(duration,currentTime);
	         	callback(currentTime,duration,timeStr);
	         });

	   },
	   //10.创建一个格式化音乐时间的方法
	   formatDate:function(duration,currentTime){
	   	var endMin=parseInt(duration/60);//结束时间时长
    	var endSec=parseInt(duration%60);
    	if(endMin<10){
    		endMin="0"+endMin;
    	}
    	if(endSec<10){
    		endSec="0"+endSec;
    	}
    	
    	var startMin=parseInt(currentTime/60);//开始时间时长
    	var startSec=parseInt(currentTime%60);
    	if(startMin<10){
    		startMin="0"+startMin;
    	}
    	if(startSec<10){
    		startSec="0"+startSec;
    	}
    	return startMin+":"+startSec+" / "+endMin+":"+endSec;
	   },
	    musicSeekTo:function(value){
	    	if(isNaN(value))return;
	    	this.audio.currentTime=this.audio.duration*value;
	    },
	    musicVoiceSeekTo:function(value){
	    	if(isNaN(value))return;
	    	if(value<0||value>1)return;
	    	//里面的参数是0-1
	    	this.audio.volume=value;
	    }
	}
	Player.prototype.init.prototype = Player.prototype;
	window.Player=Player;
})(window);
